package com.open.jack.jk_mpchart

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewGroup
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.utils.Utils
import com.open.jack.jk_mpchart.listener.IBarChartListener
import com.open.jack.jk_mpchart.model.JkChartType

class JkChart : ViewGroup, IChartBase  {
    private var chartType:String = JkChartType.UNKNOWN
    private var lineChart: JkLineChart? = null
    private var barChart:JkBarChart? = null
    private var targetChart:IChartBase? = null

    constructor(context: Context) : super(context) {
        init1(null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init1(attrs)
    }

    private fun init1(attrs: AttributeSet?) {
        setWillNotDraw(false)
        setType(chartType,attrs)
    }

    fun setType(type: String,attrs: AttributeSet? = null){
        if(this.chartType == type){
            return
        }
        this.chartType = type
        // 移除所有子视图
        removeAllViews()
        lineChart = null
        barChart = null
        targetChart = null
        if(chartType.equals(JkChartType.LINE)){
            lineChart= JkLineChart(context,attrs)
                .apply {
                    initChart()
                    layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
                }
            targetChart = lineChart
            lineChart?.let { addView(it) }
        }else if(chartType.equals(JkChartType.BAR)){
            barChart= JkBarChart(context,attrs).apply {
                initChart()
                layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
            }
            targetChart = barChart
            barChart?.let { addView(it) }
        }
        post {
            requestLayout()
            invalidate()
        }
    }


    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return targetChart?.onTouchEvent(event) ?: super.onTouchEvent(event)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        // 子View填充整个容器
        for (i in 0 until childCount) {
            val child = getChildAt(i)
            child.layout(0, 0, width, height)
        }
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        // 测量子View
        measureChildren(widthMeasureSpec, heightMeasureSpec)

        // 设置自己的尺寸
        val minSize = Utils.convertDpToPixel(200f).toInt() // 建议更大的最小尺寸
        val width = resolveSize(minSize, widthMeasureSpec)
        val height = resolveSize(minSize, heightMeasureSpec)

        setMeasuredDimension(width, height)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        // 通知子视图尺寸变化
        for (i in 0 until childCount) {
            val child = getChildAt(i)
            child.layout(0, 0, w, h)
        }
    }

    override fun onDetachedFromWindow() {
        lineChart?.onDetachedFromWindow()
        barChart?.onDetachedFromWindow()
        super.onDetachedFromWindow()
    }

    // 添加视图附加监听，确保正确显示
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        // 视图附加时立即刷新
        post {
            requestLayout()
            invalidate()
        }
    }

    fun addBarDataList(barData:List<Float>){
        barChart?.addBarDataList(barData)
    }

    fun addLineDataList(type:Int,linesData:List<Float>){
        lineChart?.addLineDataList(type,linesData)
    }

//    fun setMinOffset(offset:Float ){
//        barChart?.setMinOffset(offset)
//    }

    fun clearHighlight(){
        barChart?.clearHighlight()
    }

    fun setLineColor(type:Int,color:String) {
        lineChart?.setLineColor(type,color)
    }

    fun setBarColor(color:String){
        barChart?.setBarColor(color)
    }

    fun setBarColors(colors:List<String>){
        barChart?.setBarColors(colors)
    }

    fun setLeftAxisOffset(offset:Float ){
        barChart?.setLeftAxisOffset(offset)
    }

    fun setXAxisTextSize(size:Float){
        barChart?.setXAxisTextSize(size)
    }

    fun setLeftAxisTextSize(size:Float){
        barChart?.setLeftAxisTextSize(size)
    }

    fun setLeftAxiGridColor(color:String){
        barChart?.setLeftAxiGridColor(color)
    }

    fun setBarChartListener(listener: IBarChartListener){
        barChart?.setBarChartListener(listener)
    }

    override fun initChart() {
        targetChart?.initChart()
    }

    override fun setLeftAxisTextColor(color: String?) {
        targetChart?.setLeftAxisTextColor(color)
    }

    override fun setXAxisTextColor(color: String?) {
        targetChart?.setXAxisTextColor(color)
    }

    override fun setXAxisLabel(xIndex: Int, label: String?) {
        targetChart?.setXAxisLabel(xIndex,label)
    }

    override fun setMinOffset(offset: Float) {
        targetChart?.setMinOffset(offset)
    }

    override fun updateView() {
        targetChart?.updateView()
        // 确保在主线程刷新
        post {
            invalidate()
            requestLayout()
        }
    }


}